﻿using DbProject;
//using EasyProjectDB.Models;
//using Myapp.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace MvcEasyProject.Controllers
{
    public class SubmitTimeCardController : Controller
    {
        //
        // GET: /SubmitTimeCard/

        [AllowAnonymous]
        public ActionResult SignOut()
        {
            var ThisTime = DateTime.Now;
            using (var db = new DbProjectContext())
            {
                var CurrentWorker = db.Workers.FirstOrDefault(w => w.UserName == Environment.UserName);
                //if(ThisTime.Date == db.TimeStamps.Last(t=>t.WorkerId == CurrentWorker.WorkerId).LogIn.AddDays(1).Date)
                //{

                //}
                foreach (var row in db.TimeStamps.Where(Wt => Wt.WorkerId == CurrentWorker.WorkerId))
                {
                    if(row.LogIn.Date == ThisTime.Date)
                    row.LogOut = ThisTime;
                    if (row.LogOut.Hour == row.LogIn.Hour)
                    {
                        row.NoSignReasons = 1;
                       
                        
                    }
                    db.Entry(row).State = EntityState.Modified;
                    
                }
                db.SaveChanges();
            }

            return RedirectToAction("Index", "Home");
        }


        //public ActionResult AddTimeStamp(UserModels User)
        //{
        //      LogInOutModels timestamp = new LogInOutModels();

        //      timestamp.LogInOutUserId = WebSecurity.CurrentUserId.ToString();
        //      timestamp.LogInOutDate = DateTime.Now.Date;
        //      timestamp.User = User;
        //      if (User != null && timestamp.LogInOutEntryTime != null) 
        //      {
        //          return RedirectToAction("Index", "Home");
        //      }
        //    ModelState.AddModelError("", "לא הוחתם כרטיס");
        //    return RedirectToAction("Index", "Home");
        //}
        //
        // GET: /SubmitTimeCard/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /SubmitTimeCard/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /SubmitTimeCard/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SubmitTimeCard/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /SubmitTimeCard/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SubmitTimeCard/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /SubmitTimeCard/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
